package com.icode.web.validator;

import com.icode.core.dto.house.HouseInfoDTO;
import com.icode.core.dto.house.HouseWeekDTO;
import com.icode.core.dto.manager.ReserveDTO;
import com.icode.service.house.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-4-23
 * Time: 上午10:27
 * To change this template use File | Settings | File Templates.
 */
@Component
public class ReserveFormValidator implements Validator {

    @Autowired
    private HouseService houseService;

    @Override
    public boolean supports(Class<?> clazz) {
        return ReserveDTO.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        ReserveDTO reserveDTO = (ReserveDTO) target;
        String IDCard = reserveDTO.getIDCard();
        String phoneNum = reserveDTO.getPhoneNum();
        String email = reserveDTO.getEmail();
        String peers = reserveDTO.getPeers();

        HouseInfoDTO houseInfoDTO = houseService.getHouseInfoByGuid(reserveDTO.getHouseInfoGuid());
        HouseWeekDTO houseWeekDTO = houseService.findHouseWeekByGuid(reserveDTO.getHouseWeekGuid());
        reserveDTO.initReserveDTO(houseInfoDTO, houseWeekDTO);

        if (Pattern.matches("[\\d]{17}[\\w]{1}|[\\d]{15}", IDCard)) {
            if (Pattern.matches("[\\d]{17}[\\w]{1}", IDCard)) {
                if (!idCardValidate(IDCard)) {
                    errors.rejectValue("IDCard", null, "身份证不合法!");
                }
            }
        } else {
            errors.rejectValue("IDCard", null, "身份证不合法!");
        }
        if (!Pattern.matches("[\\d]{11}", phoneNum)) {
            errors.rejectValue("phoneNum", null, "手机号不正确!");
        }
        if (!Pattern.matches("[\\w[\\.-]]+@[\\w]+\\.com", email)) {
            errors.rejectValue("email", null, "邮箱不合法!");
        }
        if (!Pattern.matches("[\\d]+", peers)) {
            errors.rejectValue("peers", null, "同行人数不正确!");
        }
    }

    private boolean idCardValidate(String idCard) {
        String num = lastNum(idCard);
        if (num.equals(idCard.substring(17))) {
            return true;
        }
        return false;
    }

    private String lastNum(String idCard) {
        String result = "";
        String str = idCard.substring(0, 17);
        char[] array = str.toCharArray();
        int[] data = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
        int sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum += Integer.parseInt(array[i] + "") * data[i];
        }
        int a = sum % 11;
        switch (a) {
            case 0:
                result = "1";
                break;
            case 1:
                result = "0";
                break;
            case 2:
                result = "x";
                break;
            case 3:
                result = "9";
                break;
            case 4:
                result = "8";
                break;
            case 5:
                result = "7";
                break;
            case 6:
                result = "6";
                break;
            case 7:
                result = "5";
                break;
            case 8:
                result = "4";
                break;
            case 9:
                result = "3";
                break;
            case 10:
                result = "2";
                break;
        }
        return result;
    }
}
